﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using _4T.Data;
using System.Globalization;
using System.Transactions;

namespace _4T.DAL
{
    public class ProfileFilterInfoDAL
    {
        public ProfileFilterInfoDAL() { }

        public int InsertProfileFilterInfo(int profileInfoID, bool isAccept, string filterContent,
                                                string filterer)
        {
            int result = 0;
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                try
                {
                    conn.Open();
                    cmd.CommandText = "ProfileInfoInsert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ProfileInfoID", SqlDbType.Int).Value = profileInfoID;
                    cmd.Parameters.Add("@IsAccept", SqlDbType.Bit).Value = isAccept;
                    cmd.Parameters.Add("@Filterer", SqlDbType.VarChar, 50).Value = filterer;
                    cmd.Parameters.Add("@FilterContent", SqlDbType.NText).Value = filterContent;
                    cmd.Connection = conn;

                    result = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }

        public CMS_ProfileFilterInfo GetProfileFilterInfo(string profileInfoCode)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;
                CMS_ProfileFilterInfo profileFilterInfo = null;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection conn = new SqlConnection(conString);
                    SqlCommand cmd = new SqlCommand();
                    SqlDataReader reader = null;
                    try
                    {
                        conn.Open();
                        cmd.CommandText = "ProfileFilterInfoGet";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@ProfileInfoCode", SqlDbType.VarChar, 50).Value = profileInfoCode;
                        cmd.Connection = conn;

                        reader = cmd.ExecuteReader();

                        if (reader.HasRows)
                        {
                            reader.Read();
                            profileFilterInfo = GetProfileFilterInfoFromReader(reader);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        cmd.Dispose();
                        if (conn.State != ConnectionState.Closed)
                            conn.Close();
                    }
                    scope.Complete();
                }

                return profileFilterInfo;
            }

            return null;
        }

        private CMS_ProfileFilterInfo GetProfileFilterInfoFromReader(SqlDataReader reader)
        {
            string profileInfoCode = string.Empty;
            if (reader.GetValue(0) != DBNull.Value)
                profileInfoCode = reader.GetString(0);

            bool isAccept = false;
            if (reader.GetValue(1) != DBNull.Value)
                isAccept = reader.GetBoolean(1);

            string filterContent = string.Empty;
            if (reader.GetValue(2) != DBNull.Value)
                filterContent = reader.GetString(2);

            string filterer = string.Empty;
            if (reader.GetValue(3) != DBNull.Value)
                filterer = reader.GetString(3);

            string filterTime = string.Empty;
            if (reader.GetValue(4) != DBNull.Value)
                filterTime = reader.GetDateTime(4).ToString("G", CultureInfo.CreateSpecificCulture("vi-VI"));

            return new CMS_ProfileFilterInfo(profileInfoCode, filterContent, isAccept, filterer, filterTime);
        }
    }
}